{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\n",
      "127.0.0.1 - - [29/Mar/2018 16:24:41] \"GET /error HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [29/Mar/2018 16:24:42] \"GET /error HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [29/Mar/2018 16:24:43] \"GET /error HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [29/Mar/2018 16:24:46] \"GET /error HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [29/Mar/2018 16:24:47] \"GET /error HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [29/Mar/2018 16:24:48] \"GET /error HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [29/Mar/2018 16:24:49] \"GET /error HTTP/1.1\" 404 -\n"
     ]
    }
   ],
   "source": [
    "from flask import Flask,Response,jsonify,request,abort\n",
    "from flask_cors import *\n",
    "import json\n",
    "\n",
    "\n",
    "class MyResponse(Response):\n",
    "    @classmethod\n",
    "    def force_type(cls, response, environ=None):\n",
    "        if isinstance(response, (list, dict)):\n",
    "            response = jsonify(response)\n",
    "        return super(Response, cls).force_type(response, environ)\n",
    "    \n",
    "app = Flask(__name__)\n",
    "CORS(app, supports_credentials=True)\n",
    "app.response_class = MyResponse\n",
    "\n",
    "@app.route('/')\n",
    "def hello_world():\n",
    "    return 'Hello World!'\n",
    "\n",
    "#get param \n",
    "@app.route('/json',methods=['GET'])\n",
    "def get_and_return_Json():\n",
    "    t = {\n",
    "        'a': 1,\n",
    "        'b': 2,\n",
    "        'c': [3, 4, 5],\n",
    "        'param':request.args\n",
    "    }\n",
    "    return t\n",
    "\n",
    "@app.route('/json',methods=['POST'])\n",
    "def post_and_return_Json():\n",
    "    datax = request.form.to_dict()\n",
    "    t = {\n",
    "        'a': 1,\n",
    "        'c': [3, 4, 5],\n",
    "        'param':request.args,\n",
    "        'post_data':datax\n",
    "    }\n",
    "    return t\n",
    "\n",
    "@app.route('/error',methods=['GET'])\n",
    "def  get_error_Json():\n",
    "    abort(404)\n",
    "    return {}\n",
    "\n",
    "\n",
    "@app.errorhandler(403)    \n",
    "def page_not_found(error):      \n",
    "    return {\"error_code\": \"403\"}    \n",
    "   \n",
    "@app.errorhandler(404)    \n",
    "def page_not_found(error):     \n",
    "    return {\"error_code\": \"404\"}    \n",
    "\n",
    "if __name__ == '__main__':\n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
